//
//  LCMineViewController.m
//  TheLCNews
//
//  Created by apple on 15/11/26.
//  Copyright © 2015年 LC. All rights reserved.
//

#import "LCMineViewController.h"
#import "LYLCalendarViewController.h"
#import "LYLScanViewController.h"
#import "LYLStowViewController.h"
#import "LYLMapViewController.h"
#import "LCDayAndNightCell.h"
#import "LCSendMess.h"
#import "LC3DTouch.h"

@interface LCMineViewController ()<UITableViewDataSource,UITableViewDelegate,UIViewControllerPreviewingDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, strong) UIView *headeView;


@end

@implementation LCMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    [self creatView];
    [self creatHeader];
    self.array = [NSMutableArray arrayWithObjects:@"日历",@"扫一扫",@"收藏",@"地图",@"夜间模式",@"清理缓存",@"联系我们",@"3DTOUCH",nil];
    [self check3dtouch];
}
-(void)check3dtouch
{
    if(self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable)
    {
        NSLog(@"3DTouch 可用");
    }else{
        NSLog(@"3DTouch 不可用");
    }
}

#pragma mark - 头部
- (void)creatHeader
{
//    self.headeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
//    self.headeView.backgroundColor = [UIColor greenColor];
//    self.tableView.tableHeaderView = self.headeView;
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"newss"]];
    
    imageView.frame = CGRectMake(0, - HEIGHT / 3.5,WIDTH, HEIGHT / 3.5);
    
    //这个属性是把tableView往指定方向进行平移 上左下右
    self.tableView.contentInset = UIEdgeInsetsMake(HEIGHT / 3.5, 0, 0, 0);
    [self.tableView addSubview:imageView];
    imageView.tag = 1000;


}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //self.array = [NSMutableArray array];
    }
    return self;
}

#pragma mark - 创建 tableVIew
- (void)creatView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT-64) style:0 ];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
//    self.tableView.rowHeight = HEIGHT / 10.;
     self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    [self.view addSubview:_tableView];
}

#pragma mark - 返回个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

#pragma mark 重用
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 4) {
        static NSString *day = @"day";
        LCDayAndNightCell *cell = [tableView dequeueReusableCellWithIdentifier:day];
        if (cell == nil) {
            cell = [[LCDayAndNightCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:day];
            //cell.imageView.image = [UIImage imageNamed:@"iconfont-yejianmoshi"];
        }

        return cell;

    }else{
    static NSString *reuse = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse];
    }
    cell.textLabel.text = self.array[indexPath.row];
    cell.textLabel.textAlignment = 1;
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-rili"];
    } else if (indexPath.row == 1)
    {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-saoyisao"];

    } else if (indexPath.row == 2)
    {
        cell.imageView.image = [UIImage imageNamed:@"shoucang"];
    } else if (indexPath.row == 3)
    {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-map"];

    } else if (indexPath.row == 4)
    {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-yejianmoshi"];
    } else if (indexPath.row == 5)
    {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-qingchuhuancun"];
    } else if (indexPath.row == 6)
    {
        cell.imageView.image = [UIImage imageNamed:@"iconfont-neiyelianxiwomen01"];
    }
        return cell;
    }
    
}

#pragma mark - 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        LYLCalendarViewController *calendar = [[LYLCalendarViewController alloc] init];
        calendar.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:calendar animated:YES];
    } else if (indexPath.row == 1)
    {
        LYLScanViewController *scan = [[LYLScanViewController alloc] init];
        scan.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:scan animated:YES];
    } else if (indexPath.row == 2)
    {
        LYLStowViewController *stow = [[LYLStowViewController alloc] init];
        stow.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:stow animated:YES];
    } else if (indexPath.row == 3)
    {
        LYLMapViewController *map = [[LYLMapViewController alloc] init];
        map.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:map animated:YES];
    }else if (indexPath.row == 5){
        //清理缓存
        // 创建一个文件管理者
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSLog(@"%@", cachePath);
        CGFloat cacheSize = [self folderSizeAtPath:cachePath];
        
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"%@%.2fM", @"清除共", cacheSize] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        alert.delegate = self;
        [alert show];

    }else if (indexPath.row ==6){
        LCSendMess *mess = [[LCSendMess alloc] init];
        [self.navigationController pushViewController:mess animated:YES];
    }
}

//提示框的点击事件 //清理缓存
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (buttonIndex == 0) {
        NSLog(@"0");
    }else if (buttonIndex == 1) {
        // 创建一个文件管理者
        //        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        [self clearCache:cachePath];
    }
    
}


//让手机清除缓存的方法
-(void)clearCache:(NSString *)path{
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    [[SDImageCache sharedImageCache] cleanDisk];
}
//清理缓存
- (float )folderSizeAtPath:(NSString *)folderPath{
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath])
        return 0;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];
    NSString *fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize / (1024.0 * 1024.0);
}
//清理缓存

- (long long)fileSizeAtPath:(NSString *)filePath{
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    [self.tableView reloadData];
    return 0;
}




#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //先找到y的偏移量
    CGFloat yOffset = scrollView.contentOffset.y;
    
    if (yOffset < 0) {
        UIImageView *imageView = (UIImageView *)[self.tableView viewWithTag:1000];
        
        imageView.frame = CGRectMake(0, yOffset, self.view.frame.size.width, -yOffset);
    }
}
- (UIViewController *)previewingContext:(id)context viewControllerForLocation:(CGPoint) point
{
    LC3DTouch *childVC = [[LC3DTouch alloc] initWithTitle:@"测试"];
    childVC.preferredContentSize = CGSizeMake(0.0f,300.f);
    //NSLog(@"-%f = -%f",point.x,point.y);
    //    CGRect rect = CGRectMake(10, point.y - 10, self.view.frame.size.width - 20,20);
    //    context.sourceRect = rect;
    
    NSLog(@"%@",childVC);
    return childVC;
}
- (void)previewingContext:(id <UIViewControllerPreviewing>)previewingContext commitViewController:(UIViewController *)viewControllerToCommit
{
    
    [self showViewController:viewControllerToCommit sender:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
